<?php
global $smarty;

$thisPage = "static_content";

$params = unserialize(stripslashes(getRequestParameter("params")));

$staticcontentTypes = array();
$staticcontentTypes[] = array(type => "aboutus",            title => "About us",            content => getTabContent("aboutus", $smarty));
$staticcontentTypes[] = array(type => "termsofuse",         title => "Terms of use",        content => getTabcontent("termsofuse", $smarty));
$staticcontentTypes[] = array(type => "contactus",          title => "Contact us",          content => getTabContent("contactus", $smarty));
$staticcontentTypes[] = array(type => "associations",       title => "Associations",        content => getTabContent("associations", $smarty));
$staticcontentTypes[] = array(type => "businessinhungary",  title => "Business in Hungary", content => getTabContent("businessinhungary", $smarty));
$staticcontentTypes[] = array(type => "chambers",           title => "Chambers",            content => getTabContent("chambers", $smarty));
$staticcontentTypes[] = array(type => "usefullinks",        title => "Useful links",        content => getTabContent("usefullinks", $smarty));
$staticcontentTypes[] = array(type => "visithungary",       title => "Visit Hungary",       content => getTabContent("visithungary", $smarty));
$staticcontentTypes[] = array(type => "mediaoffer",         title => "Media offer",         content => getTabContent("mediaoffer", $smarty));

$titles = array();
$titles['associations'] = "association";
$titles['visithungary'] = "visitor";
$titles['chambers'] = "chamber of commerce";
$titles['usefullinks'] = "useful link";
$titles['businessinhungary'] = "business link";

addDataService("savecontent");
addDataService("getlinkgroup");
addDataService("addlinktogroup");
addDataService("removedatafromgroup");
addDataService("reorder");
addHtmlService("addpopup");
addDataService("changelink");

function buildLayout($smarty) {
    global $thisPage;
    global $params;
    global $titles;

    if ($_GET['subpage'] == "") {
        TemplateHelper::useLayout("default", true);
        $smarty->assign("default", array(
            content => getContent($smarty),
            contexthelp => getContextHelp($smarty),
        ));
    } else if ($_GET['subpage'] == "addpopup") {
        TemplateHelper::useLayout("raw", true);
        $smarty->assign("groupname", $params['groupname']);
        $smarty->assign("grouptitle", $titles[$params['groupname']]);
        $smarty->Assign("content", $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "popup_add", true)));
    } else if ($_GET['subpage'] == "changepopup") {
        TemplateHelper::useLayout("raw", true);
        initDatabase();
        Database::loadModel("staticlinks");
        $smarty->assign("groupname", $params['groupname']);
        $smarty->assign("grouptitle", $titles[$params['groupname']]);
        $smarty->assign("item", Database::getItem($params['id']));
        $smarty->Assign("content", $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "popup_change", true)));
    }
}

function executeService($serviceName, $params, $smarty) {
    switch ($serviceName) {
        case "savecontent" : { return service_savecontent($params, $smarty); }
        case "getlinkgroup" : { return service_getlinkgroup($params); }
        case "addlinktogroup" : { return service_addlinktogroup($params); }
        case "deletelinkfromgroup" : { return service_removelinkfromgroup($params); }
        case "addpopup" : { return service_addpopup($params, $smarty); }
        case "changelink" : { return service_changelink($params); }
        case "reorder" : { return service_reorder($params, $smarty); }
    }
}

function getTabContent($tabName, $smarty) {
    initDatabase();
    switch ($tabName) {
        case "aboutus" : { return getAboutusTabcontent($smarty); }
        case "termsofuse" : { return getTermsofuseTabContent($smarty); }
        case "contactus" : { return getContactusTabContent($smarty); }
        case "associations" : { return getAssociationsTabcontent($smarty); }
        case "businessinhungary" : { return getBusinessInHungaryTabContent($smarty); }
        case "chambers" : { return getChambersTabContent($smarty); }
        case "usefullinks" : { return getUsefulLinksTabContent($smarty); }
        case "visithungary" : { return getVisitHungaryTabContent($smarty); }
        case "mediaoffer" : { return getMediaOfferTabContent($smarty); }
    }
}

function getAboutUsTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("aboutus");
    $smarty->assign("aboutus_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.aboutus", true));
}

function getTermsOfUseTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("termsofuse");
    $smarty->assign("termsofuse_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.termsofuse", true));
}

function getMediaOfferTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("mediaoffer");
    $smarty->assign("mediaoffer_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.mediaoffer", true));
}

function getContactUsTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("contactus");
    $smarty->assign("contactus_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.contactus", true));
}

function getAssociationsTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("associations");
    $smarty->assign("associations_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.associations", true));
}

function getBusinessInHungaryTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("businessinhungary");
    $smarty->assign("businessinhungary_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.businessinhungary", true));
}

function getChambersTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("chambers");
    $smarty->assign("chambers_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.chambers", true));
}

function getUsefulLinksTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("usefullinks");
    $smarty->assign("usefullinks_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.usefullinks", true));
}

function getVisitHungaryTabContent($smarty) {
    global $thisPage;

    Database::loadModel("staticcontent");
    $content = Database::getModel()->getField("visithungary");
    $smarty->assign("visithungary_content", $content);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "tab.visithungary", true));
}

function getContent($smarty) {
    global $thisPage;
    global $staticcontentTypes;
    
    $smarty->assign("staticcontenttabtypes", $staticcontentTypes);
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "content", true));
}

function getContextHelp($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "contexthelp", true));
}

function service_savecontent($params, $smarty) {
    Database::loadModel("staticcontent");
    Database::update($params, true);
    return serviceSuccessful(array(
    ));
}

function service_getlinkgroup($params) {
    Database::loadModel("staticlinks");
    $list = Database::getModel()->getGroup($params['linkgroup']);
    return array(
        rows => JQGridHelper::convert($list, "id"),
        page => 1,
        total => 1,
        records => count($list),
    );
}

function service_addpopup($params, $smarty) {
    print_r($params);
}

function service_addlinktogroup($params) {
    Database::loadModel("staticlinks");
    Database::getModel()->saveLinkToGroup($params['groupname'], $params['href'], $params['title']);
    return serviceSuccessful(array(
    ));
}

function service_removelinkfromgroup($params) {
    Database::loadModel("staticlinks");
    Database::getModel()->removeLinkFromGroup($params['groupname'], $params['ids']);
    return serviceSuccessful(array(
    ));
}

function service_reorder($params) {
    Database::loadModel("staticlinks");
    Database::getModel()->reOrder($params['idlist']);
    return serviceSuccessful(array());
}

function service_changelink($params) {
    Database::loadModel("staticlinks");
    Database::getModel()->changelink($params);
    return serviceSuccessful(array());
}

function getPageHeader() {
}

function getPageFooter() {
}

?>
